import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home.vue'
import denglu from './views/denglu.vue'
import zhuce from "./views/zhuce.vue"
import xiugai from "./views/xiugai.vue"
import tuichu from "./views/tuichu.vue"
import cart from './views/cart.vue'
import order from './views/order.vue'
import bigorder from './views/bigorder.vue'
import orderlist from './views/orderlist.vue'
import list from './views/list.vue'

Vue.use(Router)

const router =new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
    },
      {
          path: '/denglu',
          name: 'denglu',
          component: denglu,
      },
      {
          path: '/list',
          name: 'list',
          component: list,
      },
      {
          path: '/zhuce',
          name: 'zhuce',
          component: zhuce,
      },
      {
          path: '/cart',
          name: 'cart',
          component: () => import('./views/cart.vue')
      },
      {
          path: '/order/:orderId',
          name: 'order',
          component: () => import('./views/order.vue')
      },
      {
          path: '/orderlist/:index',
          name: 'orderlist',
          component: () => import('./views/orderlist.vue')
      },
      {
          path: '/bigorder',
          name: 'bigorder',
          component: () => import('./views/bigorder.vue')
      },
      {
          path: '/tuichu',
          name: 'tuichu',
          component: tuichu,
      },
      {
          path: '/xiugai',
          name: 'xiugai',
          component: xiugai,
      },
      {
          path: '/road',
          name: 'road',
          component: () => import('./views/road.vue')

      },

    {
      path: '/about/:id',
      name: 'about',
      component: () => import( './views/About.vue')
    }
  ]
});
router.beforeEach((to, from, next) => {
    // var c=JSON.parse(localStorage.getItem("Authorization"))
    if (to.path === '/') {    //若要跳转的页面是登录界面
        next();     //直接跳转
    }else if(to.path==='/denglu'){
        next();
    }else if(to.path==='/zhuce'){
        next();
    }
    else if (to.path === '/xiugai'|| to.path === '/cart' || to.path === '/bigorder'){   //若要跳转的页面是个人界面
        let token = JSON.parse(localStorage.getItem("Authorization"));    //获取本地存储的token值
        console.log(token)
        if (token===null||token===''){    //若token为空则验证不成功，跳转到登录页面
            next('/denglu');
        }
        else{           //不为空则验证成功
            next();
        }
    }
    else{
        next();
    }
});

export default router;
